//
//  AddTableViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/1.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "AddTableViewController.h"
#import "AddCell.h"
#import "CityViewController.h"
#import "CityModel.h"
#import "JourneyTVController.h"
#import "DateViewController.h"

@interface AddTableViewController ()

@property (nonatomic,strong) CityModel *cityModel1;

@property (nonatomic,strong) CityModel *cityModel2;

@property (nonatomic,strong) NSMutableArray *cityArray1;

@property (nonatomic,strong) NSMutableArray *cityArray2;
/**当天日期*/
@property (nonatomic, strong) NSString *currentDate;


@end

@implementation AddTableViewController

-(instancetype)initWithStyle:(UITableViewStyle)style{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        [self addSubViews];
    }
    return self;
}
- (void)addSubViews{

}
-(NSMutableArray *)cityArray1{
    if (!_cityArray1) {
        _cityArray1 = [NSMutableArray array];
    }
    return _cityArray1;
}
-(NSMutableArray *)cityArray2{
    if (!_cityArray2) {
        _cityArray2 = [NSMutableArray array];
    }
    return _cityArray2;
}
-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置旅程";
    [self.tableView registerClass:[AddCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView reloadData];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"开始行程" style:UIBarButtonItemStylePlain target:self action:@selector(startAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar_arrow_up@2x"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
}
-(void)startAction:(UIBarButtonItem *)sender{
    JourneyTVController *journeyTVC = [[JourneyTVController alloc] init];
#pragma mark======================================================
    
    
    
    
    
    [self.navigationController  pushViewController:journeyTVC animated:YES];
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender{
    NSLog(@"..............");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"选择出发地";
    }
    return @"选择目的地";
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY/MM/dd"];
    self.currentDate = [dateformatter stringFromDate:[NSDate date]];
    
    
    AddCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.lab.text = @"出发城市";
            cell.imageV.image = [UIImage imageNamed:@"station"];
            if (self.cityArray1.count == 0) {
                cell.nameLab.text = @"请选择";
            } else {
                CityModel *m = self.cityArray1[0];
                cell.nameLab.text = m.cityName;
            }
        } else {
            cell.lab.text = @"出发日期";
            cell.imageV.image = [UIImage imageNamed:@"calendar_21.428571428571px_1194912_easyicon.net"];
                cell.nameLab.text = self.currentDate;
        }
    } else {
        if (indexPath.row == 0) {
            cell.lab.text = @"目的地";
            cell.imageV.image = [UIImage imageNamed:@"station"];
            if (self.cityArray2.count == 0) {
                cell.nameLab.text = @"请选择";
            } else {
                CityModel *m = self.cityArray2[0];
                cell.nameLab.text = m.cityName;
            }
        } else {
            cell.lab.text = @"归期";
            cell.imageV.image = [UIImage imageNamed:@"calendar_21.428571428571px_1194912_easyicon.net"];
            cell.nameLab.text = self.currentDate;
        }
    }

    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddCell *addCell = [tableView cellForRowAtIndexPath:indexPath];
    CityViewController *cityVC = [CityViewController new];
    
    cityVC.passBlock = ^(CityModel *m,NSString *name){
        if ([name isEqualToString:@"出发城市"]) {
            self.cityModel1 = m;
            [self.cityArray1 addObject:m];
        }else if ([name isEqualToString:@"目的地"]){
            self.cityModel2 = m;
            [self.cityArray2 addObject:m];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    };
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        cityVC.titleName = @"出发城市";
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        cityVC.titleName = @"目的地";
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    
    
    DateViewController *dateVC = [[DateViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:dateVC];

    if ((indexPath.section == 0 && indexPath.row == 1) || (indexPath.section == 1 && indexPath.row == 1)) {
        
        __weak typeof(self) __self = self;
        dateVC.passDateBlock = ^(NSDate *date){
            NSDate *passDate = [[DateHelper sharedDateHelper] dayDateWithDate:date AddHours:YES];
            addCell.nameLab.text = [__self stringFromDate:passDate ];
        };
        [self presentViewController:naVC animated:YES completion:nil];
    }
}
/**Data 在转变为字符串*/
- (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss zzz"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    NSString *string = [destDateString substringToIndex:10];
    return string;
}

@end
